package com.credithc.customers.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.TypeReference;
import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.message.Message;
import com.credithc.baseserv.core.message.MessageFactory;
import com.credithc.customers.api.service.ICustomerService;
import com.credithc.customers.api.utils.RequestUtils;
import com.credithc.customers.base.codes.CustomerCodes;
import com.credithc.customers.base.exception.CustomerException;
import com.credithc.customers.base.ro.*;
import com.credithc.framework.log.annotation.Trace;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @description: 客户开户
 * @author: denghx
 * @date: 2018/11/29 0:09
 * @param: null
 * @return:
 */
@RestController
@RequestMapping("/api/yzt/v1")
@Slf4j
public class CustomerController {
    @Autowired
    MessageFactory messageFactory;

    @Autowired
    ICustomerService customerService;

    @Autowired
    RequestUtils requestUtils;

    /**
     * @description: 客户开户
     * @author: denghx
     * @date: 2018/11/30 16:00
     * @param: request
     * @return: com.credithc.baseserv.core.message.Message
     */
    @PostMapping("/customerNo")
    @Trace
    public Message createCustomerNo(HttpServletRequest request) {
        try {
            String requestStr = RequestUtils.generateRequest(request);
            BaseRequest<CustomerInfoRo> bRequest = JSON.parseObject(requestStr, new TypeReference<BaseRequest<CustomerInfoRo>>() {
            });
            requestUtils.checkRequestBaseParam(requestStr);
            Map<String, String> map = customerService.createCustomerNo(bRequest);
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
        } catch (IOException e) {
            throw new CustomerException(CustomerCodes.READ_REQUEST_ERROR);
        } catch (JSONException e) {
            log.info("创建客户接口参数格式错误，异常信息：{}", e.getMessage());
            throw new CustomerException(CustomerCodes.FAST_JSON_ERROR);
        }
    }

    /**
     * @description: 验证客户实名信息
     * @author: fby
     * @date: 2020/03/06 15:07
     * @param: request
     * @return: com.credithc.baseserv.core.message.Message
     */
    @PostMapping("/customer/verify")
    @Trace
    public Message verifyCustomer(HttpServletRequest request) {
        try {
            String requestStr = RequestUtils.generateRequest(request);
            BaseRequest<CustomerIdInfoRo> bRequest = JSON.parseObject(requestStr, new TypeReference<BaseRequest<CustomerIdInfoRo>>() {
            });
//            requestUtils.checkRequestBaseParam(requestStr);
            Map<String, String> map = customerService.verifyCustomer(bRequest);
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
        } catch (IOException e) {
            throw new CustomerException(CustomerCodes.READ_REQUEST_ERROR);
        } catch (JSONException e) {
            log.info("创建客户接口参数格式错误，异常信息：{}", e.getMessage());
            throw new CustomerException(CustomerCodes.FAST_JSON_ERROR);
        }
    }

    /**
     * @description: 客户联系信息修改
     * @date: 2018/11/30 16:00
     * @param: request
     * @return: com.credithc.baseserv.core.message.Message
     */
    @PostMapping("/customerInfo/update")
    @Trace
    public Message updateCustomerNo(HttpServletRequest request) {
        try {
            String requestStr = RequestUtils.generateRequest(request);
            BaseRequest<CustomerContactModifyRo> bRequest = JSON.parseObject(requestStr, new TypeReference<BaseRequest<CustomerContactModifyRo>>() {
            });
            requestUtils.checkRequestBaseParam(requestStr);
            String customerNo = bRequest.getBody().getCustomerNo();
            Map<String, String> map = customerService.updateCustomerNo(bRequest, customerNo);
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
        } catch (IOException e) {
            throw new CustomerException(CustomerCodes.READ_REQUEST_ERROR);
        } catch (JSONException e) {
            log.info("客户信息修改接口参数格式错误，异常信息：{}", e.getMessage());
            throw new CustomerException(CustomerCodes.FAST_JSON_ERROR);
        }
    }

    /**
     * @description: 批量调用生成客户号
     * @author: denghx
     * @date: 2019/3/22 1:05
     * @param: request
     * @return: com.credithc.baseserv.core.message.Message
     */
    @PostMapping("/customerNo/batch")
    public Message batchCustomerNo(HttpServletRequest request) {
        long start = System.currentTimeMillis();
        try {
            String requestStr = RequestUtils.generateRequest(request);
            BaseBatchRequest<BatchCustomerRo> bRequest = JSON.parseObject(requestStr, new TypeReference<BaseBatchRequest<BatchCustomerRo>>() {
            });
            requestUtils.checkRequestBaseParam(requestStr);
            List<Map<String, Object>> result = customerService.batchCustomerNo(bRequest);
            log.info("===================spend============"+(System.currentTimeMillis()- start));
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, result);
        } catch (IOException e) {
            throw new CustomerException(CustomerCodes.READ_REQUEST_ERROR);
        } catch (JSONException e) {
            log.info("批量客户接口参数格式错误，异常信息：{}", e.getMessage());
            throw new CustomerException(CustomerCodes.FAST_JSON_ERROR);
        }
    }


}
